import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useLoginStore } from '@/stores/login'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      meta: {
        title: "仪表盘",
        requiresAuth: true
      },
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      meta:{
        title: "关于",
        requiresAuth: true
      },
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/login',
      name: 'login',
      meta:{
        title: "登录",
        requiresAuth: true
      },
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/courseList',
      name: 'courseList',
      meta:{
        title: "全部课程",
        requiresAuth: true
      },
      component: () => import('../views/course/courseListView.vue')
    },
    {
      path: '/video',
      name: 'video',
      meta:{
        title:"课程视频",
        requiresAuth:true
      },
      component: () => import('../views/course/VideoView.vue')
    },
    {
      path: '/courseDetail',
      name: 'courseDetail',
      meta:{
        title: "详情",
        requiresAuth: true
      },
      component: () => import('../views/course/courseDetailView.vue')
    },
    {
      path: '/teacher',
      name: 'teacher',
      meta:{
        title: "讲师",
        requiresAuth: true
      },
      component: () => import('../views/teacher/teacherListView.vue')
    },
    {
      path: '/teacherTodetail',
      name: 'teacherTodetail',
      meta:{
        title: "讲师详情",
        requiresAuth: true
      },
      component: () => import('../views/teacher/teacherTodetail.vue')
    },
    {
      path: '/essayList',
      name: 'essayList',
      meta:{
        title: "文章",
        requiresAuth: true
      },
      component: () => import('../views/essay/essayList.vue')
    }, 
    
    {
      path: '/essayTodetail',
      name: 'essayTodetail',
      meta:{
        title: "文章详情",
        requiresAuth: true
      },
      component: () => import('../views/essay/essayTodetail.vue')
    }, 
    {
      path: '/myUser',
      name: 'myUser',
      meta:{
        title: "个人中心",
        requiresAuth: true
      },
      component: () => import('../views/user/myUser.vue')
    }, 
  ]
})

router.beforeEach((to, from, next) => {
  const loginStore = useLoginStore();
  const userInfo = localStorage.getItem("userInfoClient");

  if (loginStore.userInfo || userInfo) {
      next();
  } else {
      if (to.path === "/login") {
          next();
      } else {
          next("/login");
      }
  }
});
  export default router
